from trade import models
import uuid

def option_buyHistory(nid,domain_name, username, now_time, buy_type, price):
  """
  修改与添加用户的购买记录
  :param nid:
  :param domain_name:
  :param username:
  :param now_time:
  :param buy_type:
  :param price:
  :return:
  """
  db_user_exist = models.UserInfo.objects.filter(user_email=username).first()
  if db_user_exist:
    buy_dic_db = str(db_user_exist.purchased)
    buy_dic = eval(buy_dic_db)
    update_dic = {
      'id': nid,
      'date': now_time,
      'price': price,
      'type': buy_type
    }
    buy_dic[domain_name] = update_dic
    models.UserInfo.objects.filter(user_email=username).update(purchased=buy_dic)
  else:
    uid = models.UsersList.objects.filter(user_email=username).first().uuid
    create_dic = {
      'user_email': username,
      'purchased': {domain_name: {
                      'id': nid,
                      'date': now_time,
                      'price': price,
                      'type': buy_type}},
      'email_uuid_id': uid
    }
    models.UserInfo.objects.create(**create_dic)
  return
